import { createRouter, createWebHashHistory } from 'vue-router';
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import CourseList from '../views/CourseList.vue';
import CourseDetail from '../views/CourseDetail.vue';
import AddCategory from '../views/AddCategory.vue';
import AddCourse from '../views/AddCourse.vue';
import AddTask from '../views/AddTask.vue';
import { useAuthStore } from '../store/auth';

const routes = [
  { path: '/', redirect: '/courses' },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/courses', component: CourseList },
  { path: '/courses/:id', component: CourseDetail, props: true },
  { 
    path: '/add-category', 
    component: AddCategory, 
    meta: { requiresAuth: true },
  },
  { 
    path: '/add-course', 
    component: AddCourse, 
    meta: { requiresAuth: true },
  },
  { 
    path: '/add-task', 
    component: AddTask, 
    meta: { requiresAuth: true },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to) => {
  if (to.meta.requiresAuth) {
    const auth = useAuthStore();
    if (!auth.isLoggedIn) {
      return '/login';
    }
  }
});

export default router;